/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package BUS;
import DTO.CFields;
import DTO.Discussion;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class DiscussionBUS extends IData<Discussion>{

    @Override
    public Discussion add(Discussion item) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    protected void add(Discussion item, int t) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void saveBatch() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void addrows(List<Discussion> list, SubConfig config) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Discussion Find(Discussion item, List<Discussion> list) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    protected Object OnSubmit(Discussion item, int t, SubConfig config) {
        Object result = 0;
        String mysql ="INSERT INTO discussion(" + CFields.project_id + "," + CFields.user_id +","+CFields.description+","+CFields.path+","+CFields.create_date+")" + " VALUES (?,?,?,?,?);";                      
        try {   
            PreparedStatement cs;
            if(t == 0){                
                cs = config.connec.prepareStatement(mysql,PreparedStatement.RETURN_GENERATED_KEYS);
                cs.setLong(1,item.getProject_id());
                cs.setLong(2,item.getUser_id());
                cs.setString(3, item.getDescription());
                cs.setString(4, item.getPath());
                cs.setTimestamp(5, item.getCreate_date());
                cs.executeUpdate();
                ResultSet keyResultSet = cs.getGeneratedKeys();
                if (keyResultSet.next()) {
                    result = (long) keyResultSet.getInt(1);      
                }
            }
            else if(t== 1){
                mysql = "update discussion set " + CFields.project_id+ "=?,"
                        + CFields.user_id+ "=?,"+CFields.description+"=?,"+CFields.path+"=?,"+CFields.create_date+"=?, where " + CFields.id + "=?";
                cs = config.connec.prepareStatement(mysql);
                cs.setLong(1,item.getProject_id());
                cs.setLong(2,item.getUser_id());
                cs.setString(3, item.getDescription());
                cs.setString(4, item.getPath());
                cs.setTimestamp(5, item.getCreate_date());  
                cs.setLong(8, item.getId());
                result = cs.executeUpdate();
            }
            else{
                
                mysql = "delete from discussion where "+ CFields.id + "=?";
                cs = config.connec.prepareStatement(mysql);
                cs.setLong(1, item.getId());
                result = cs.executeUpdate();
            }
        } catch (SQLException ex) {            
            Logger.getLogger(DiscussionBUS.class.getName()).log(Level.SEVERE, null, ex);            
        }        
        return result;
    }

    @Override
    public Object insertOnSubmit(Discussion item, SubConfig config) {
        return this.OnSubmit(item, 0, config);
    }

    @Override
    public Object updateOnSubmint(Discussion item, SubConfig config) {
        return this.OnSubmit(item, 1, config);
    }

    @Override
    public Object deleteOnSubmint(Discussion item, SubConfig config) {
        return this.OnSubmit(item, 2, config);
    }

    @Override
    protected Discussion getItem(ResultSet rs) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public List<Discussion> getAll(SubConfig config) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
}
